package writeToExcel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import constants.Constants;

public class WritePrecisionToExcel {

	SXSSFWorkbook wb = new SXSSFWorkbook(-1);
	
	public WritePrecisionToExcel(String dataset, String fold,
			double[][] posPrecision, double[][] negPrecision,
			double[][] posRecall, double[][] negRecall, double[][] posF1,
			double[][] negF1) throws IOException {
		String[] epsilons = { "0", "05", "1", "15", "2", "25" };

		String excel_dir = Constants.excel_dir;
		
		Sheet sh = wb.createSheet("Classification Metrics");
		
		createNewSheet(sh, "Positive Precision", epsilons, posPrecision, 0, 0);
		createNewSheet(sh, "Negative Precision", epsilons, negPrecision, 1, 0);
		createNewSheet(sh, "Positive Recall", epsilons, posRecall, 0, 1);
		createNewSheet(sh, "Negative Recall", epsilons, negRecall, 1, 1);
		createNewSheet(sh, "Positive F1", epsilons, posF1, 0, 2);
		createNewSheet(sh, "Negative F1", epsilons, negF1, 1, 2);

		FileOutputStream file = new FileOutputStream(new File(excel_dir
				+ dataset + "-fold-" + fold + "-Classification-Metrics.xlsx"));
		wb.write(file);
	}
	
	private void createNewSheet(Sheet sh, String sheetName, String[] epsilons, double[][] data, int x, int y) {
		Row row0;
		if((row0 = sh.getRow(0+7*x)) == null) {
			row0 = sh.createRow(0+7*x);
		}
		Cell cell00 = row0.createCell(6*y);
		cell00.setCellValue(sheetName);
		for (int j = 1; j <= 5; j++) {
			Cell cell = row0.createCell(j+6*y);
			cell.setCellValue("margin = " + (0.0 + 0.5 * (j - 1)));
		}
		for (int i = 1; i <= 6; i++) {
			Row row;
			if((row = sh.getRow(i+7*x)) == null) {
				row = sh.createRow(i+7*x);
			}
			Cell cell0 = row.createCell(0+6*y);
			cell0.setCellValue("0." + epsilons[i - 1]);
			for (int j = 1; j <= 5; j++) {
				Cell cell = row.createCell(j+6*y);
				cell.setCellValue(Double.parseDouble(String.format("%.1f",
						data[i - 1][j - 1])));
			}
		}
		
	}

}
